//
//  CZMultiSettingViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/8.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZMultiSettingViewController.h"

@interface CZMultiSettingViewController () <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UITextField *changeTF;
@property (nonatomic, strong) UIPickerView *pickerView;

@end

@implementation CZMultiSettingViewController {
    UIWindow *_window;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = GLOBAL_BACKGROUND_COLOR;
    UIButton *rightBarBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBarBtn.frame = CGRectMake(0, 0, 54, 25);
    rightBarBtn.backgroundColor = mRGBColor(214, 221, 220);
    [rightBarBtn addTarget:self action:@selector(changeInfo) forControlEvents:(UIControlEventTouchUpInside)];
    [rightBarBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    rightBarBtn.layer.cornerRadius = 2;
    rightBarBtn.clipsToBounds = YES;
    rightBarBtn.tintColor = ThemeColor;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
    self.changeTF.borderStyle = UITextBorderStyleNone;
    self.changeTF.delegate = self;
    
    if ([self.changeType isEqualToString:@"nickname"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_nickname"];
        self.changeTF.placeholder = @"请输入新的昵称";
        self.title = @"更改昵称";
    } else if ([self.changeType isEqualToString:@"sex"]) {
        self.iconView.image = [UIImage imageNamed:@"my_iocn_sex"];
        _window = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
        _window.backgroundColor = mRGBACOLOR(0, 0, 0, 0.5);
        _window.windowLevel = UIWindowLevelAlert;
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handTap:)];
        [_window addGestureRecognizer:tap];
        _pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, 220, 180)];
        [_pickerView setBackgroundColor:mRGBColor(245, 245, 245)];
        _pickerView.delegate=self;
        _pickerView.dataSource=self;
        _pickerView.showsSelectionIndicator=YES;
        [CZNsjHelper SetTopcornerRadius:_pickerView];
        [_window addSubview:_pickerView];
        _pickerView.center = _window.center;

        UIButton* okBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, _pickerView.bottom, 220, 40)];
        [okBtn setBackgroundColor:ThemeColor];
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        okBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [CZNsjHelper SetBottomcornerRadius:okBtn];
        [okBtn addTarget:self action:@selector(onclickOKBtn) forControlEvents:UIControlEventTouchUpInside];
        [_window addSubview:okBtn];
        okBtn.centerX = _window.centerX;
        
        self.title = @"更改性别";
        self.changeTF.placeholder = @"请选择性别";
    } else if ([self.changeType isEqualToString:@"age"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_age"];
        self.changeTF.placeholder = @"请输入年龄";
        self.title = @"更改年龄";
    } else if ([self.changeType isEqualToString:@"qq"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_qq"];
        self.changeTF.placeholder = @"请输入新的QQ";
        self.title = @"更改QQ";
    } else if ([self.changeType isEqualToString:@"guardian_name"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_guardian"];
        self.changeTF.placeholder = @"请输入监护人的姓名";
        self.title = @"更改监护人姓名";
    } else if ([self.changeType isEqualToString:@"guardian_phone"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_guarPhone"];
        self.changeTF.placeholder = @"请输入监护人的电话";
        self.title = @"更改监护人电话";
    } else if ([self.changeType isEqualToString:@"logo"]) {
        self.iconView.image = [UIImage imageNamed:@"my_icon_guarPhone"];
    }
    
    // Do any additional setup after loading the view from its nib.
}

- (void)onclickOKBtn {
//    [_pickerView removeFromSuperview];
    _pickerView.hidden = YES;
    _window.hidden = YES;
//    _window = nil;
    
}
- (void)handTap:(UITapGestureRecognizer*)tap {
    _window.hidden = YES;
    _pickerView.hidden = YES;
}



- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([self.changeType isEqualToString:@"sex"]) {
        _window.hidden = NO;
        _pickerView.hidden = NO;
        [_window makeKeyAndVisible];
//        _pickerView.center = self.view.center;
        return NO;
    }
    return YES;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return row == 0 ? @"帅哥" : @"妹子";
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 2;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.changeTF.text = row == 0 ? @"帅哥" : @"妹子";
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.alpha = 1;
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
}
- (void)changeInfo {
    [self.view endEditing:YES];
    [SVProgressHUD showWithStatus:@"保存中"];
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user-info/update-info",APP_HOST];
    NSMutableDictionary *parmaDic = @{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token]}.mutableCopy;
    if ([self.changeType isEqualToString:@"nickname"]) {
        if (self.changeTF.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入新的昵称"];
            return;
        }  else {
            [parmaDic setObject:self.changeTF.text forKey:@"nickname"];
        }
    } else if ([self.changeType isEqualToString:@"sex"]) {
        
        if ([self.changeTF.text isEqualToString:@"帅哥"]) {
            [parmaDic setObject:@1 forKey:@"sex"];
        } else if ([self.changeTF.text isEqualToString:@"妹子"]) {
            [parmaDic setObject:@2 forKey:@"sex"];
        } else {
            [SVProgressHUD showErrorWithStatus:@"请选择性别"];
            return;
        }
        
    } else if ([self.changeType isEqualToString:@"age"]) {
        if (self.changeTF.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入年龄"];
            return;
        }  else {
            [parmaDic setObject:self.changeTF.text forKey:@"age"];
        }
    } else if ([self.changeType isEqualToString:@"qq"]) {
        if (self.changeTF.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入新的QQ"];
            return;
        }  else {
            [parmaDic setObject:self.changeTF.text forKey:@"qq"];
        }
    } else if ([self.changeType isEqualToString:@"guardian_name"]) {
        if (self.changeTF.text.length < 1) {
            [SVProgressHUD showErrorWithStatus:@"请输入监护人姓名"];
            return;
        }  else {
            [parmaDic setObject:self.changeTF.text forKey:@"guardian_name"];
        }
    } else if ([self.changeType isEqualToString:@"guardian_phone"]) {
        if (self.changeTF.text.length != 11) {
            [SVProgressHUD showErrorWithStatus:@"请输入正确监护人电话"];
            return;
        }  else {
            [parmaDic setObject:self.changeTF.text forKey:@"guardian_phone"];
        }
    }
    [GCZAFNetWorking POST:urlString params:parmaDic success:^(NSDictionary *response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:@"修改资料成功"];
            self.backBlock(YES);
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

- (void)dealloc {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
